package saves;

import game.*;
import game.Controls.Key;
import static game.Controls.Key.*;
import images.ImageInfo;
import images.ImageManager;
import images.ItemLoader;
import images.MenuLoader;
import images.ObjectLoader;
import images.ShipLoader;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import math.Point;
import menu.Menu;
import org.lwjgl.input.Keyboard;
import static org.lwjgl.opengl.GL11.*;
import org.newdawn.slick.Color;
import saves.items.Equipamento;
import saves.items.Item;
import saves.items.Municao;
import saves.objects.Bot;
import saves.objects.Corpo;
import saves.objects.Nave;
import screen.Dialog;
import units.GameTime;

public final class Save implements Serializable {

    private static final long serialVersionUID = 4740115637787871001L;
    private String nome;
    private int codSave;
    private int nivelAtual, sistemaAtual, corpoAtual;
    //Levels from nivelAtual:
    //Level 0 - Galaxy | sistemaAtual is where it's hovering
    //Level 1 - System | sistemaAtual is where it is and corpoAtual is where it's hovering
    //Level 2 - Orb | the ship is within corpoAtual in sistemaAtual
    private int[] areaAtual;
    //Substituir por Nave (?)
    private double zoom, angulo, deltaAngulo, velocidade;
    private Point posicaoGalaxia, posicaoSistema;
    private Area area;
    private Nave player;
    private ArrayList<EntidadeNave> naves;
    private Faccao[] faccoes;
    private GameTime tempoAtual;
    
    private QuickAccess[] quickAccess;
    
    private int codGalaxia;
    private transient Galaxia gx;
    
    private ArrayList<Relacao> relNaves, relFaccoes;

    private HashMap<Integer, EntidadeNave> navesConhecidas;
    private SistemaEstelar[] informacoesConhecidas;
    private HashMap<Integer, Area> areasConhecidas; //relaciona o código da área com seu objeto
    
    private int status;
    private Dialog dialog; //Current poped-up dialog

    private Save() {
    }

    public Save(String nome, int naveJogador) {
        this(0, nome, naveJogador);
    }

    public Save(int codigo, String nome, int naveJogador) {
        this.nome = nome;
        this.codSave = codigo;
        
        this.quickAccess = new QuickAccess [4];
        for (int i = 0; i < this.quickAccess.length; i++)
            this.quickAccess[i] = null;
        
        System.out.println("-- Gerando galáxia...");
        long t1 = System.currentTimeMillis();
        GalaxiaInicial g = GalaxyGenerator.gerarGalaxia(null);
        long t2 = System.currentTimeMillis();
        System.out.println("-- Gerada. Operação realizada em " + (t2 - t1) + "ms.");

        //Salve the galaxy
        gx = new Galaxia(g.getSistemasEstelares(), g.getCrafting());
        gx.usadaEm.add(codSave);
        System.out.println("-- Salvando em arquivo...");
        t1 = System.currentTimeMillis();
        //codGalaxia = GalaxyManager.create(nome, gx);
        t2 = System.currentTimeMillis();
        System.out.println("-- Salvo em " + (t2 - t1) + "ms.");

        //Salve each star's info
        System.out.println("-- Salvando as informações mutáveis...");
        t1 = System.currentTimeMillis();
        //GalaxyManager.saveAllInfo(nome, codGalaxia, codSave);
        t2 = System.currentTimeMillis();
        System.out.println("-- Salvo em " + (t2 - t1) + "ms.");

        informacoesConhecidas = new SistemaEstelar[g.getSistemasEstelares().length];
        
        sistemaAtual = g.getSistemaInicial();
        corpoAtual = g.getCorpoInicial();
        areaAtual = g.getAreaInicial();
        if (corpoAtual != -1) { //The player is already in an orb
            if (areaAtual == null)
                areaAtual = new int[]{gx.sistemasEstelares[sistemaAtual].getCorpo(corpoAtual).getCodArea()};
            area = Area.carregarArea(nome, codSave, areaAtual);
            if (area == null) //It was never generated
                area = GalaxyGenerator.gerarAreas(nome, gx.sistemasEstelares[sistemaAtual].getCorpo(corpoAtual))[0];
        }
        tempoAtual = g.getTempoInicial();
        faccoes = g.getFaccoes();

        int[] stats = {2, 2, 2, 2, 2, 2};
        switch (naveJogador) {
            case 0:
                stats[1] += 1;
                stats[2] += 2;
                break;
            case 1:
                stats[0] += 1;
                stats[3] += 2;
                break;
            case 2:
                stats[4] += 4;
                stats[5] += 4;
                break;
        }
        EntidadeNave playerEntidade = new EntidadeNave(nome, Data.PLANETA_TERENT, null, stats, Data.FACCAO_TERENT);
        playerEntidade.setEquips(ShipLoader.DEFAULT_EQUIPS[naveJogador]);
        naves = new ArrayList<>();
        naves.add(playerEntidade);

        if (area != null) {
            player = new Nave(20, 20, playerEntidade, area.getWorld());//g.getPontoInicial().x, g.getPontoInicial().y, playerEntidade, area.getWorld());
            area.addObjeto(player);
            area.setReferencia();
        }

        this.nivelAtual = 0; //TODO change!!
        this.zoom = 3;
        this.angulo = this.deltaAngulo = this.velocidade = 0;
        
        informacoesConhecidas = new SistemaEstelar[g.getSistemasEstelares().length];
        areasConhecidas = new HashMap<>();
        navesConhecidas = new HashMap<>();
        
        this.posicaoGalaxia = new Point();
        this.status = 0;
        this.getPlayer().replenish();
    }
    
    public Area getCurrentKnownArea() {
        return this.areasConhecidas.get(areaAtual[0]);
    }
    
    public Area getCurrentArea() {
        return this.area;
    }
    
    public int getNivelAtual() {
        return this.nivelAtual;
    }

    public String getNome() {
        return this.nome;
    }

    public Crafting getCrafting() {
        return gx.crafting;
    }
    
    public EntidadeNave getPlayer() {
        return naves.get(0);
    }

    public Faccao[] getFaccoes() {
        return faccoes;
    }
    
    public QuickAccess[] getQuickAccess() {
        return this.quickAccess;
    }

    public void setAc(int ac) {
        if (area != null)
            area.setAc(ac);
        else
            //Galaxy / System
            this.velocidade = 1.5 * ac;
    }

    public void girar(int cw) {
        if (area != null)
            area.girar(cw);
        else
            //Galaxy / System
            this.deltaAngulo = cw * Nave.ANG_VEL;
    }

    public void move(boolean up) {
        if (area != null) {
            //TODO
        } else
            if (up)
                zoom *= 2;
            else
                zoom /= 2;
    }

    public void go(boolean up) {
        if (nivelAtual == 0 && !up && sistemaAtual != -1) {
            //Entrar em sistema
            nivelAtual++;
            posicaoSistema = new Point(); //Volta a estar no centro
            velocidade = 0;
        } else if (nivelAtual == 1 && up) {
            nivelAtual--;
            velocidade = 0;
        } else if (nivelAtual == 1 && !up && corpoAtual != -1) {
            //Entrar em corpo
            nivelAtual++;
            
            areaAtual = new int[]{gx.sistemasEstelares[sistemaAtual].getCorpo(corpoAtual).getCodArea()};
            area = Area.carregarArea(nome, codSave, areaAtual);
            if (area == null) //Nunca foi gerada
                area = GalaxyGenerator.gerarAreas(nome, gx.sistemasEstelares[sistemaAtual].getCorpo(corpoAtual))[0];
            
            player = new Nave(15, 15, naves.get(0), area.getWorld());
            area.addObjeto(player);
            area.setReferencia();
        } else if (area != null && up) {
            nivelAtual--;
            area = null;
            velocidade = 0;
        } else if (area != null && area.getSelecao() != null)
            openSelectionDialog();
    }
    
    private void openSelectionDialog() {
        status = 2;
        String[] it = area.getSelecao().getInterfaces();
        String[] btns = new String[2 + it.length];
        btns[0] = area.isLocked() ? "Unlock" : "Lock";
        btns[btns.length - 1] = "Cancel";
        for (int i = 1; i < btns.length - 1; i++)
            btns[i] = it[i - 1];
        dialog = new Dialog(3*Configurations.SCREEN_WIDTH/4, area.getSelecao().getDescription(), btns);
    }
    
    public boolean inDialog() {
        return status != 0;
    }
    
    public Menu parse(int k, char c) {
        if (status == 2) {
            //asset dialog != null
            Key key = Controls.get(k, true);
            if (key == SEL_GO) {
                int it = dialog.getSelectedItem();
                if (it == 0) //lock/unlock
                    area.setLocked(!area.isLocked());
                else if (it != area.getSelecao().getInterfaces().length + 1) {
                    Menu m = area.getSelecao().getInterface(it - 1, getPlayer());
                    dialog = null;
                    status = 0;
                    return m;
                }
                
                dialog = null;
                status = 0;
            } else if (key == SEL_BACK) { //do nothing
                dialog = null;
                status = 1;
            } else
                dialog.parse(k, c);
        } else if (status == 1) {
            Key key = Controls.get(k, true);
            
            ArrayList<Corpo> sels = area.getSelectables();
            int pos;
            for (pos = 0; pos < sels.size(); pos++)
                if (sels.get(pos) == area.getSelecao())
                    break;
            
            if (key == TALK || key == SEL_BACK)
                status = 0;
            
            else if (key == SEL_UP) {
                if (pos > 0)
                    area.setSelecao(sels.get(pos - 1));
            } else if (key == SEL_DOWN) {
                if (pos < sels.size() - 1)
                    area.setSelecao(sels.get(pos + 1));                
            } else if (key == SEL_GO)
                openSelectionDialog();
        } else {
            Key key = Controls.get(k, false);
            if (key == ACCELERATE)
                setAc(1);
            else if (key == BREAK)
                setAc(-1);

            else if (key == TURN_RIGHT)
                girar(1);
            else if (key == TURN_LEFT)
                girar(-1);

            else if (key == GO_UP)
                go(true);
            else if (key == GO_DOWN)
                go(false);

            else if (key == ZOOM_IN)
                move(true);
            else if (key == ZOOM_OUT)
                move(false);

            else if (key == QA_0)
                quickAccess(0);
            else if (key == QA_1)
                quickAccess(1);
            else if (key == QA_2)
                quickAccess(2);
            else if (key == QA_3)
                quickAccess(3);
            
            else if (key == TALK) {
                if (area != null && !area.getSelectables().isEmpty()) {
                    status = 1;
                    if (area.getSelecao() == null)
                        area.setSelecao(area.getSelectables().get(0));
                }
            }

            //TODO remove!! \/
            else if (key == COLLECT) { //R
                if (area != null) {
                    EntidadeNave joca = new EntidadeNave("Joca", null, null, new int[]{3, 2, 2, 2, 2, 2}, null);
                    joca.setEquips(ShipLoader.DEFAULT_EQUIPS[0]);
                    joca.replenish();
                    area.getObjetos().add(new Bot(50, 20, joca, area.getWorld()));
                }
                //getPlayer().getInventory().addItem(new Consumivel("Maçã", "Uma maçã.", 0, new Efeito[]{new Efeito(0, 0, 0, 10, -1)}, new Condicao[0]));
                getPlayer().getInventory().addItem(new Municao(0), 300);
                getPlayer().getInventory().addItem(new Municao(1), 15);
                getPlayer().getInventory().addItem(new Municao(2), 10);
                getPlayer().getInventory().addItem(new Municao(3), 5);
                //*
                //add some equips
                Iterator<Integer> it = ShipLoader.modelos.keySet().iterator();
                while (it.hasNext())
                    getPlayer().getInventory().addItem(new Equipamento(it.next(), 2, 1, 1));
                //*/
            } else if (k == Keyboard.KEY_Y) {
                player.getEntidade().ganharExp(200);
            }
            //TODO remove!! /\
        }
        return null;
    }

    public void carregarGalaxia() throws IOException {
        try {
            gx = GalaxyManager.load(nome, codGalaxia, codSave);
        } catch (ClassNotFoundException e) {}
    }
    
    public boolean hasToLoad() {
        return this.gx == null;
    }
    
    public void drawPlayer(int x, int y, float a, int t, int state) {
        ShipLoader.bindTexture();
        ShipLoader.draw(naves.get(0), x, y, t, a, state);
    }

    public void desenhar() { //Mounts the final screen of the game; it gets the area image (if any) and adds HUD, and other suff
        if (area != null) { //Inside an area
            Area knwonArea = areasConhecidas.get(areaAtual[0]);
            if (status != 0)
                ImageManager.bindGray();
            ImageManager.bindLights();
            knwonArea = area.desenhar(knwonArea);
            ImageManager.unbindLights();
            if (status != 0) {
                ImageManager.unbindGray();
                //draw paused icon
                ImageInfo.drawColorSquare(Configurations.SCREEN_WIDTH - 98, 10, 32, 100, new java.awt.Color(127, 0, 0), true);
                ImageInfo.drawColorSquare(Configurations.SCREEN_WIDTH - 42, 10, 32, 100, new java.awt.Color(127, 0, 0), true);
            }
            areasConhecidas.put(areaAtual[0], knwonArea);
        } else if (nivelAtual == 0) {
            //Galaxy map
            int t = (int) (32 * zoom / 3);
            if (t < 3)
                t = 3;

            int se = -1;
            float ldist = 0;
            double inx = (Configurations.SCREEN_WIDTH + t) / (zoom * 2), iny = (Configurations.SCREEN_HEIGHT + t) / (zoom * 2);
            for (int i = 0; i < gx.sistemasEstelares.length; i++)
                if (Math.abs(gx.sistemasEstelares[i].getPosicao().x - posicaoGalaxia.x) <= inx && Math.abs(gx.sistemasEstelares[i].getPosicao().y - posicaoGalaxia.y) <= iny) {
                    ObjectLoader.drawStar(gx.sistemasEstelares[i].getEstrelas()[0].getTipo(), Configurations.SCREEN_WIDTH / 2 + (int) Math.round((gx.sistemasEstelares[i].getPosicao().x - posicaoGalaxia.x) * zoom), Configurations.SCREEN_HEIGHT / 2 + (int) Math.round((gx.sistemasEstelares[i].getPosicao().y - posicaoGalaxia.y) * zoom), t, t);
                    float dist = gx.sistemasEstelares[i].getPosicao().dist(posicaoGalaxia);
                    if (dist < ldist || se == -1) {
                        se = i;
                        ldist = dist;
                    }
                }
            if (se != -1 && ldist < 10)
                sistemaAtual = se;
            else
                sistemaAtual = -1;

            int s = 32;
            //Draw the player
            drawPlayer((Configurations.SCREEN_WIDTH - s) / 2, (Configurations.SCREEN_HEIGHT - s) / 2, (int) angulo, s, 0);
        } else {
            //System map
            int t = 32;
            ObjectLoader.drawStar(gx.sistemasEstelares[sistemaAtual].getEstrelas()[0].getTipo(), (Configurations.SCREEN_WIDTH - t)/2, (Configurations.SCREEN_HEIGHT - t)/2, t, t);
            
            glDisable(GL_TEXTURE);
            glDisable(GL_BLEND);

            CorpoOrbitavel[] ps = gx.sistemasEstelares[sistemaAtual].getPlanetas();
            int co = -1;
            float ldist = 0;
            for (int i = 0; i < ps.length; i++) {
                Point p = ps[i].getPosicao(tempoAtual);
                //See if it's hovering
                float dist = p.dist(posicaoSistema);
                if (dist < ldist || co == -1) {
                    co = i;
                    ldist = dist;
                }
                ps[i].desenhar(p, zoom);
            }
            
            if (co != -1 && ldist < 5000)
                corpoAtual = co;
            else
                corpoAtual = -1;

            Color.white.bind();
            glEnable(GL_TEXTURE);
            glEnable(GL_BLEND);
            
            int s = 24;
            //Draw the player's ship
            drawPlayer(Configurations.SCREEN_WIDTH/2 + (int) (zoom * posicaoSistema.x/1000) - s/2, Configurations.SCREEN_HEIGHT/2 + (int) (zoom * posicaoSistema.y/1000) - s/2, (int) angulo, s, 0);
        }

        /*
         * Draw HUD
         */
        ObjectLoader.drawHUD();

        //Section 1
        drawPlayer(10, Configurations.GAME_HEIGHT + 10, 0, 48, 0);

        int s = 130;
        float vida = getPlayer().getFracaoVida();
        if (vida < 0)
            FontLoader.TEXT_SMALLEST.drawString(65, Configurations.GAME_HEIGHT + 10, "Disabled: " + (int) (-100*vida) + "%");
        else
            ObjectLoader.drawBar(0, 65, Configurations.GAME_HEIGHT + 10, (int) (s * vida), s, 10);
        ObjectLoader.drawBar(1, 65, Configurations.GAME_HEIGHT + 25, (int) (s * naves.get(0).getFracaoEnergia()), s, 10);
        ObjectLoader.drawBar(2, 65, Configurations.GAME_HEIGHT + 40, (int) (s * naves.get(0).getFracaoXP()), s, 10);
        
        FontLoader.TEXT_BOLD.drawString(10, Configurations.GAME_HEIGHT + 50, nome);
        FontLoader.TEXT.drawString(10, Configurations.GAME_HEIGHT + 65, "Level " + naves.get(0).getNivel());
        
        int ns = 32, dx = 65 + (119 - 3*ns)/2;
        MenuLoader.bindTexture();
        Point.Inteiro[] pos = MenuLoader.drawQuickAccess(dx, Configurations.GAME_HEIGHT + 65, ns);
        
        ItemLoader.bindTexture();
        for (int i = 0; i < pos.length; i++)
            if (quickAccess[i] != null)
                quickAccess[i].draw(pos[i].x, pos[i].y, ns, getPlayer());
        
        //Section 2
        final int si = 64;
        if (area != null) {
            ArrayList<Corpo> lista = area.getSelectables();
            Corpo sel = area.getSelecao();
            int h = Configurations.GAME_HEIGHT + 35;
            int desv = 217 + 18 + si;
            FontLoader.TEXT_BOLD.drawString(desv, Configurations.GAME_HEIGHT + 10, "Interact with:");
            if (!lista.isEmpty()) {
                for (int i = 0; i < lista.size(); i++) {
                    if (lista.get(i) == sel)
                        h += 15*i;
                    FontLoader.TEXT.drawString(desv, Configurations.GAME_HEIGHT + 25 + 15*i, lista.get(i).toString(), status == 0 ? Color.lightGray : Color.white);
                }
                //draw bullet and image
                if (sel != null) {
                    MenuLoader.bindTexture();
                    MenuLoader.drawSelector(228 + si, h);
                    if (sel instanceof Nave) {
                        ShipLoader.bindTexture();
                        ShipLoader.draw(((Nave) sel).getEntidade(), 220, Configurations.GAME_HEIGHT + 10, si, 0, 0);
                        //draw life bar for ships
                        vida = ((Nave) sel).getEntidade().getFracaoVida();
                        if (vida < 0)
                            FontLoader.TEXT_SMALLEST.drawString(220, Configurations.GAME_HEIGHT + 15 + si, "Disabled: " + (int) (-100*vida) + "%");
                        else
                            ObjectLoader.drawBar(0, 220, Configurations.GAME_HEIGHT + 15 + si, (int) (si * vida), si, 10);
                    } else
                        sel.draw(220 + si/2, Configurations.GAME_HEIGHT + 10 + si/2, si, si);
                }
                Color.white.bind();
            } else
                FontLoader.TEXT.drawString(desv, h, "No object found.");
        }
        FontLoader.TEXT_BOLD.drawString(217, Configurations.GAME_HEIGHT + 120, "Log");
        
        //Section 3
        if (area != null) {
            //Delegar para a área
            areasConhecidas.get(areaAtual[0]).desenharMapa(620, Configurations.GAME_HEIGHT + 10, 96, 96, 1, false); //todo change mini-mapa mini-mode
        } else if (nivelAtual == 0) {
            //Galaxy
            if (sistemaAtual != -1) {
                FontLoader.TEXT_BOLD.drawString(652, Configurations.GAME_HEIGHT + 10, (gx.sistemasEstelares[sistemaAtual].getNome() == null ? "Sistema Estelar " + sistemaAtual : gx.sistemasEstelares[sistemaAtual].getNome()));
                int i;
                ArrayList<Faccao> facs = new ArrayList<>();
                for (i = 0; i < gx.sistemasEstelares[sistemaAtual].getPlanetas().length; i++) {
                    CorpoOrbitavel p = gx.sistemasEstelares[sistemaAtual].getPlanetas()[i];
                    if (!p.isCompleteInfo())
                        p.loadInfo(SaveManager.SAVE_PATH + nome + "\\saves\\" + codSave + "\\areas\\");
                    //Escolher a cor de acordo com o lifeState
                    Color c = Color.white;
                    switch (p.getCorpoInfo().getLifeState()) {
                        case 0:
                            c = new Color(180, 180, 180);
                            break;
                        case 1:
                            c = new Color(153, 217, 134);
                            break;
                        case 2:
                            c = new Color(153, 217, 234);
                            break;
                    }
                    Faccao fac = p.getCorpoInfo().getFaccaoControle();
                    if (fac != null && !facs.contains(fac)) //TODO testar!!
                        facs.add(fac);       //It was 657 \/
                    FontLoader.TEXT.drawString(630, Configurations.GAME_HEIGHT + 50 + 20 * i, (p.getNome() == null ? /*"Planeta " + p.getCodArea() : p.getNome()*/ "" : "") + (fac == null ? "" : " (" + fac.getNome() + ")") + p.getTheta(tempoAtual), c);
                    switch (p.getCorpoInfo().getAttackState()) {
                        //Draw swords' icon
                        case 1:
                            break;
                        case 2:
                            break;
                    }
                }

                if (facs.isEmpty())
                    FontLoader.TEXT.drawString(652, Configurations.GAME_HEIGHT + 25, "Inabitado");
                else {
                    String t = facs.get(0).getNome();
                    for (int k = 1; k < facs.size(); k++)
                        t += ", " + facs.get(i).getNome();
                    FontLoader.TEXT.drawString(652, Configurations.GAME_HEIGHT + 25, t);
                }
            }
        } else {
            //Sistema
            if (corpoAtual != -1) {
                FontLoader.TEXT_BOLD.drawString(652, Configurations.GAME_HEIGHT + 10, (gx.sistemasEstelares[sistemaAtual].getPlanetas()[corpoAtual].getNome() == null ? "Planeta " + corpoAtual : gx.sistemasEstelares[sistemaAtual].getPlanetas()[corpoAtual].getNome()));
            }
        }
        
        //finally
        if (dialog != null)
            dialog.desenhar();
    }

    public void atualizar(int deltaT) {
        if (status != 0)
            return;
        tempoAtual.addSegundos(20 * deltaT);
        if (area != null)
            area.update(deltaT);
        else { //If the player is not inside an area
            angulo += deltaAngulo / 5;
            angulo %= 360;

            naves.get(0).consumir(-0.005f); //Painel solar (TODO ver eficiencia do ps da nave & luminosidade recebida no momento)
            Point p;
            float c;
            if (nivelAtual == 0) {
                p = posicaoGalaxia;
                c = 1;
            } else {
                p = posicaoSistema;
                c = 500;
            }
            if (naves.get(0).consumir((float) Math.abs(velocidade) / 100)) {
                p.x += Math.sin(Math.toRadians(angulo)) * velocidade * c;
                p.y -= Math.cos(Math.toRadians(angulo)) * velocidade * c;
            }
        }
    }
    
    public void quickAccess(int pos) {
        if (quickAccess[pos] != null) {
            Item i = quickAccess[pos].getItem();
            if (i != null)
                getPlayer().usar(i);
            else if (area != null) {
                Nave pl = (Nave) area.getReferencia();
                switch (quickAccess[pos].upgrade) {
                    case 0: //main laser
                        pl.atirarLaser(0);
                        break;
                    case 1: //sec laser
                        pl.atirarLaser(1);
                        break;
                    case 18: //ultra laser
                        pl.atirarLaser(2);
                        break;
                    case 2: //machine gun
                        pl.atirarProjetil(0, area);
                        break;
                    case 3: //missile
                        pl.atirarProjetil(1, area);
                        break;
                    case 7: //bombs dispenser
                        pl.atirarProjetil(2, area);
                        break;
                    case 11: //bombs with gc
                        pl.atirarProjetil(3, area);
                        break;
                }
            }
        }
    }
    
    public void setQuickAccess(int code, Item item) {
        this.quickAccess[code] = new QuickAccess(item);
    }
    
    public void setQuickAccess(int code, int upgrade) {
        this.quickAccess[code] = new QuickAccess(upgrade);
    }

    public static class QuickAccess implements Serializable {
        private static final long serialVersionUID = 1L;
        
        private Item item;
        private int upgrade;
        
        private QuickAccess() {}
        
        public QuickAccess(Item item) {
            this.item = item;
            this.upgrade = -1;
        }
        
        public QuickAccess(int upgrade) {
            this.item = null;
            this.upgrade = upgrade;
        }

        public Item getItem() {
            return item;
        }

        public int getUpgrade() {
            return upgrade;
        }
        
        public void draw(int x, int y, int s, EntidadeNave n) {
            if (item != null) {
                if (item.getTipo() == 1) { //equip
                    ShipLoader.bindTexture();
                    ItemLoader.drawEquip((Equipamento) item, x, y, s);
                } else {
                    ItemLoader.bindTexture();
                    ItemLoader.drawItem(item.getIcone(), x, y, s);
                }
                int qtd = n.getInventory().qtosItems(item);
                if (qtd != 1) {
                    String t = String.valueOf(qtd);
                    Color c;
                    if (qtd == 0)
                        c = Color.red;
                    else
                        c = Color.white;
                    FontLoader.TEXT.drawString(x + s - FontLoader.TEXT.getWidth(t), y + s - FontLoader.TEXT.getLineHeight(), t, c);
                    if (qtd == 0)
                        Color.white.bind();
                }
            } else {
                ItemLoader.bindTexture();
                ItemLoader.drawUpgrade(upgrade, x, y, s);
            }
        }
        
        @Override
        public String toString() {
            if (item == null)
                return TextManager.t().UPGRADES_NAMES[upgrade];
            return item.getNome();
        }
    }
}